package cn.fxl.pet.service.impl;

import cn.fxl.basic.service.impl.BasicServiceImpl;
import cn.fxl.pet.domain.PetType;
import cn.fxl.pet.mapper.PetTypeMapper;
import cn.fxl.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BasicServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> getPetTree() {
        List<PetType> petTypes = petTypeMapper.getAll();
        HashMap<Long, PetType> map = new HashMap<>();
        for (PetType petType : petTypes) {
            map.put(petType.getId(),petType);
        }
        ArrayList<PetType> petTree = new ArrayList<>();
        for (PetType petType : petTypes) {
            if(petType.getPid()==null){
                petTree.add(petType);
            }else {
                PetType petType1 = map.get(petType.getPid());
                List<PetType> children = petType1.getChildren();
                children.add(petType);
            }
        }
        return petTree;
    }

}
